import java.security.MessageDigest;

public class MD5 {

    // 将字节转成str
    public static String byteToHexString(byte[] by) {
        StringBuffer SB = new StringBuffer();
        int i = 0;
        while (i < by.length) {
            int k = by[i];
            int j = k;
            if (k < 0) {
                j = k + 256;
            } if (j < 16) {
                SB.append("0");
            }
            SB.append(Integer.toHexString(j));
            i += 1;
        }
        return SB.toString();
    }

    public static void main(String[] args) throws Exception {
        // getInstance: 获取单例 (单例设计模式)
        // MD5
        MessageDigest md = MessageDigest.getInstance("MD5");
        // 加密的内容必须是一个字节
        md.update("a12345678".getBytes());
        // 解密结果放在字节数组
        byte[] res = md.digest();
        System.out.println(byteToHexString(res));
    }
}